package com.b2b.controller;

import com.b2b.model.entity.WorkOrder;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.b2b.exception.CustomException;
import com.b2b.exception.CustomUnauthorizedException;
import com.b2b.model.WorkAdministratorUserDto;
import com.b2b.model.WorkOrderDto;
import com.b2b.model.common.BaseDto;
import com.b2b.model.common.ResponseBean;
import com.b2b.service.WorkAdministratorUserService;
import com.b2b.service.WorkOrderService;
import com.b2b.util.WorKOrderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/workOrder")
@PropertySource("classpath:config.properties")
public class WorkOrderController {
    @Value("${refreshTokenExpireTime}")
    private String refreshTokenExpireTime;

    private final WorkOrderService workOrderService;

    private final WorkAdministratorUserService workAdministratorUserService;

    @Autowired
    public WorkOrderController(WorkOrderService workOrderService, WorkAdministratorUserService workAdministratorUser){
        this.workOrderService = workOrderService;
        this.workAdministratorUserService = workAdministratorUser;
    }

    @Autowired
    WorKOrderUtil worKOrderUtil;

    @GetMapping("/getAll")
    public ResponseBean workOrder(BaseDto baseDto){
        if (baseDto.getPage() == null || baseDto.getRows() == null) {
            baseDto.setPage(1);
            baseDto.setRows(10);
        }
        PageHelper.startPage(baseDto.getPage(), baseDto.getRows());
        List<WorkOrder> workOrders = workOrderService.selectAll();
        PageInfo<WorkOrder> selectPage = new PageInfo<WorkOrder>(workOrders);
        if (workOrders == null ) {
            throw new CustomException("查询失败(Query Failure)");
        }
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("count", selectPage.getTotal());
        result.put("data", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
    }
    @PostMapping("/add")
    public ResponseBean addWorkOrder(@RequestBody WorkOrderDto workOrderDto) {
        Integer count = worKOrderUtil.insertWorkOrder(workOrderDto);

        if (count <= 0){
            throw new CustomException("新增失败(Insert Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "新增成功(Insert Success)", count);
    }

    @PutMapping("/update")
    public ResponseBean updateWordOrder(@RequestBody WorkOrderDto workOrderDto){
        WorkOrder workOrderTemp = new WorkOrder();
        workOrderTemp.setWorkOrderId(workOrderDto.getWorkOrderId());
        Integer count = workOrderService.selectCount(workOrderTemp);
        if (count <= 0){
            throw new CustomUnauthorizedException("该单号不存在(Account not exist.)");
        }
        count = worKOrderUtil.updateWorkOrder(workOrderDto);
        if (count <= 0){
            throw new CustomException("更新失败(Update Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "更新成功(Update Success)", workOrderDto);
    }

    @DeleteMapping
    public ResponseBean deleteWordOrder(WorkOrderDto workOrderDto){
        WorkOrder workOrderTemp = new WorkOrder();
        workOrderTemp.setWorkOrderId(workOrderDto.getWorkOrderId());
        Integer count = workOrderService.selectCount(workOrderTemp);
        if (count <= 0){
            throw new CustomUnauthorizedException("该单号不存在(Account not exist.)");
        }
        count = workOrderService.deleteByPrimaryKey(workOrderDto.getWorkOrderId());
        if (count <= 0){
            throw new CustomUnauthorizedException("删除失败");
        }
        return new ResponseBean(HttpStatus.OK.value(), "删除成功(Delete Success)", null);
    }
}
